package com.fei.springcloud.controller;

import com.fei.springcloud.pojo.Dept;
import com.fei.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private DiscoveryClient clientl;

    @PostMapping("/addDept")
    public boolean addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }

    @GetMapping("/queryAll")
    public List<Dept> queryById(){
        return deptService.queryAll();
    }

    @GetMapping("/discovery")
    public Object discovery(){
        List<String> services = clientl.getServices();
        System.out.println(services);
        List<ServiceInstance> instances = clientl.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getServiceId()+"\t"
            );
        }
        return this.clientl;
    }
}
